package org.example.jdbc;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * JDBC执行查询语句
 * 查询所有用户的用户名、密码、年龄;
 */
public class Demo04JDBC {
    public static void main(String[] args) {
        //自动关闭特性
        try (Connection connection = DBUtil.getConnection();){
            Statement statement = connection.createStatement();
            /*
                1.写操作[增删改]：executeUpdate()  返回值为int 类型
                2.读操作[查]：executeQuery() 返回值为ResultSet 集合
                  resultSet.next():
                  1.判断是否存在下一个元素，存在返回true,否则返回false;
                  2.游标cursor初始位置在第一个元素之前;
             */
            ResultSet resultSet = statement.executeQuery("SELECT username,password,age FROM user");
            while (resultSet.next()) {
                String username = resultSet.getString("username");
                String password = resultSet.getString("password");
                int age = resultSet.getInt("age");
                System.out.println(username + " " + password + " " + age);
            }
            System.out.println(resultSet);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
